#include "imagecoloradjusterTextWidget.h"

ImageColorAdjusterTextWidget::ImageColorAdjusterTextWidget(QWidget *parent)
{
    this->setFixedSize(800,600) ;

    // 在窗口构造函数或初始化时设置   这个属性会让窗口在接收到closeEvent后自动销毁，适用于大多数场景。
    setAttribute(Qt::WA_DeleteOnClose);


    // 创建布局
    QVBoxLayout *layout = new QVBoxLayout(this);

    // 原始图片标签
    QLabel *originalLabel = new QLabel("yuan tp ", this);
    originalLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(originalLabel);

    // 调整后图片标签
    QLabel *adjustedLabel = new QLabel("xin图片", this);
    adjustedLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(adjustedLabel);

    // 加载按钮
    QPushButton *loadButton = new QPushButton("加载", this);
    layout->addWidget(loadButton);

    // 色温调整 (-50 至 50)
    QHBoxLayout *tempLayout = new QHBoxLayout();
    QLabel *tempLabel = new QLabel("色温:", this);
    QSlider *tempSlider = new QSlider(Qt::Horizontal, this);
    tempSlider->setRange(-50, 50);
    tempSlider->setValue(0);
    tempSlider->setTickInterval(1); // 刻度间隔
    tempSlider->setTickPosition(QSlider::TicksBelow); // 刻度位置
    QLabel *tempValueLabel = new QLabel(QString::number(tempSlider->value()), this);
    tempLayout->addWidget(tempLabel);
    tempLayout->addWidget(tempSlider);
    tempLayout->addWidget(tempValueLabel);
    layout->addLayout(tempLayout);

    // 色调调整 (-50 至 50)
    QHBoxLayout *hueLayout = new QHBoxLayout();
    QLabel *hueLabel = new QLabel("色调:", this);
    QSlider *hueSlider = new QSlider(Qt::Horizontal, this);
    hueSlider->setRange(-50, 50);
    hueSlider->setValue(0);
    hueSlider->setTickInterval(1);
    hueSlider->setTickPosition(QSlider::TicksBelow);
    QLabel *hueValueLabel = new QLabel(QString::number(hueSlider->value()), this);
    hueLayout->addWidget(hueLabel);
    hueLayout->addWidget(hueSlider);
    hueLayout->addWidget(hueValueLabel);
    layout->addLayout(hueLayout);

    // 饱和度调整 (0.0 至 2.0，通过缩放实现)
    QHBoxLayout *satLayout = new QHBoxLayout();
    QLabel *satLabel = new QLabel("饱和度：", this);
    QSlider *satSlider = new QSlider(Qt::Horizontal, this);
    satSlider->setRange(-50, 50); // 实际值 = 滑块值 / 10.0
    satSlider->setValue(0); // 初始值1.0
    satSlider->setTickInterval(1);
    satSlider->setTickPosition(QSlider::TicksBelow);
    QLabel *satValueLabel = new QLabel(QString::number(satSlider->value()), this);
    satLayout->addWidget(satLabel);
    satLayout->addWidget(satSlider);
    satLayout->addWidget(satValueLabel);
    layout->addLayout(satLayout);

    // 亮度调整 (-100 至 100)
    QHBoxLayout *brightLayout = new QHBoxLayout();
    QLabel *brightLabel = new QLabel("亮度:", this);
    QSlider *brightSlider = new QSlider(Qt::Horizontal, this);
    brightSlider->setRange(-100, 100);
    brightSlider->setValue(0);
    brightSlider->setTickInterval(20);
    brightSlider->setTickPosition(QSlider::TicksBelow);
    QLabel *brightValueLabel = new QLabel(QString::number(brightSlider->value()), this);
    brightLayout->addWidget(brightLabel);
    brightLayout->addWidget(brightSlider);
    brightLayout->addWidget(brightValueLabel);
    layout->addLayout(brightLayout);

    satLabel;
    tempLabel->setFixedSize(100,32);
    hueLabel->setFixedSize(100,32);
    satLabel->setFixedSize(100,32);
    brightLabel->setFixedSize(100,32);


    tempValueLabel->setFixedSize(64,32);
    hueValueLabel->setFixedSize(64,32);
    satValueLabel->setFixedSize(64,32);
    brightValueLabel->setFixedSize(64,32);
    // 使用样式表设置右对齐
    tempValueLabel->setStyleSheet("QLabel { qproperty-alignment: AlignRight; }");
    hueValueLabel->setStyleSheet("QLabel { qproperty-alignment: AlignRight; }");
    satValueLabel->setStyleSheet("QLabel { qproperty-alignment: AlignRight; }");
    brightValueLabel->setStyleSheet("QLabel { qproperty-alignment: AlignRight; }");
    // 添加值变化关联（关键：滑块移动时实时更新标签）
    connect(tempSlider, &QSlider::valueChanged, tempValueLabel, [=](int value) {
        tempValueLabel->setText(QString::number(value));
    });
    connect(hueSlider, &QSlider::valueChanged, hueValueLabel, [=](int value) {
        hueValueLabel->setText(QString::number(value));
    });
    connect(satSlider, &QSlider::valueChanged, satValueLabel, [=](int value) {
        // 转换为0.0-2.0格式，保留1位小数
        satValueLabel->setText(QString::number(value));
    });
    connect(brightSlider, &QSlider::valueChanged, brightValueLabel, [=](int value) {
        brightValueLabel->setText(QString::number(value));
    });



    // 调整按钮
    QPushButton *adjustButton = new QPushButton("应用调整", this);
    layout->addWidget(adjustButton);

    adjustButton->show();
    // 保存按钮
    QPushButton *saveButton = new QPushButton("保存图片", this);
    layout->addWidget(saveButton);



    QObject::connect(tempSlider, &QSlider::valueChanged , [=]{
        if (!currentImage.isNull()) {
            QImage adjustedImage = ImageColorAdjuster::adjustImage(
                currentImage,
                tempSlider->value(),
                hueSlider->value(),
                satSlider->value(),
                brightSlider->value()
                );

            adjustedLabel->setPixmap(QPixmap::fromImage(adjustedImage).scaled(
                adjustedLabel->width(), adjustedLabel->height(),
                Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }

    }) ;

    QObject::connect(hueSlider, &QSlider::valueChanged , [=]{
        if (!currentImage.isNull()) {
            QImage adjustedImage = ImageColorAdjuster::adjustImage(
                currentImage,
                tempSlider->value(),
                hueSlider->value(),
                satSlider->value(),
                brightSlider->value()
                );

            adjustedLabel->setPixmap(QPixmap::fromImage(adjustedImage).scaled(
                adjustedLabel->width(), adjustedLabel->height(),
                Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }

    }) ;

    QObject::connect(satSlider, &QSlider::valueChanged , [=]{
        if (!currentImage.isNull()) {
            QImage adjustedImage = ImageColorAdjuster::adjustImage(
                currentImage,
                tempSlider->value(),
                hueSlider->value(),
                satSlider->value(),
                brightSlider->value()
                );

            adjustedLabel->setPixmap(QPixmap::fromImage(adjustedImage).scaled(
                adjustedLabel->width(), adjustedLabel->height(),
                Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }

    }) ;

    QObject::connect(brightSlider, &QSlider::valueChanged , [=]{
        if (!currentImage.isNull()) {
            QImage adjustedImage = ImageColorAdjuster::adjustImage(
                currentImage,
                tempSlider->value(),
                hueSlider->value(),
                satSlider->value(),
                brightSlider->value()
                );

            adjustedLabel->setPixmap(QPixmap::fromImage(adjustedImage).scaled(
                adjustedLabel->width(), adjustedLabel->height(),
                Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }

    }) ;



    // 连接加载按钮信号
    QObject::connect(loadButton, &QPushButton::clicked, [=]() {
        QString fileName = QFileDialog::getOpenFileName(this, "selete file", "", "图片文件 (*.png *.jpg *.jpeg)");
        if (!fileName.isEmpty()) {
            currentImage.load(fileName);
            originalLabel->setPixmap(QPixmap::fromImage(currentImage).scaled(
                originalLabel->width(), originalLabel->height(),
                Qt::KeepAspectRatio, Qt::SmoothTransformation));
            adjustedLabel->setText("xin tu p");
        }
    });

    // 连接调整按钮信号
    QObject::connect(adjustButton, &QPushButton::clicked, [=]() {
        if (!currentImage.isNull()) {
            QImage adjustedImage = ImageColorAdjuster::adjustImage(
                currentImage,
                tempSlider->value(),
                hueSlider->value(),
                satSlider->value(),
                brightSlider->value()
                );

            adjustedLabel->setPixmap(QPixmap::fromImage(adjustedImage).scaled(
                adjustedLabel->width(), adjustedLabel->height(),
                Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
    });

    // 连接保存按钮信号
    QObject::connect(saveButton, &QPushButton::clicked, [=]() {
        if (!currentImage.isNull() && adjustedLabel->pixmap() && !adjustedLabel->pixmap()->isNull()) {
            QString fileName = QFileDialog::getSaveFileName(this, "保存图片", "", "PNG 文件 (*.png);;JPEG 文件 (*.jpg)");
            if (!fileName.isEmpty()) {
                adjustedLabel->pixmap()->save(fileName);
            }
        }
    });



    this->setLayout(layout) ;




}

ImageColorAdjusterTextWidget::~ImageColorAdjusterTextWidget()
{

}
